package leetcode.L142;

import java.util.HashMap;
import java.util.List;

class ListNode {
  int val;
  ListNode next;
  ListNode(int x) {
      val = x;
      next = null;
  }
}

public class Solution {
    public ListNode detectCycle(ListNode head) {
        HashMap<ListNode, Integer> map = new HashMap<>();
        ListNode pos = head;
        while (pos!=null) {
            if (map.containsKey(pos)) {
                return pos;
            }
            map.put(pos, pos.val);
            pos=pos.next;
        }
        return null;
    }
}